package com.dlc.felear.xiaoensale.utils;

import android.content.Context;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.OkHttpManager;
import com.dlc.felear.xiaoensale.helper.ReqAdData;
import com.itdlc.android.library.utils.APPDidUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.simple.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class FaceScanUtil {
    private static String TAG = "FaceScanUtil";
    private Context context;
    private static FaceScanUtil instance;
    private boolean isAvailable = false;
    private static final int NO_INIT_CODE=271378620;

    public static FaceScanUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (FaceScanUtil.class) {
                if (instance == null) {
                    instance = new FaceScanUtil(context);
                }
            }
        }
        return instance;
    }


    private FaceScanUtil(Context context) {
        this.context = context;
        initWx();
    }

    public void initWx() {
//        WxPayFace.getInstance().initWxpayface(context, new IWxPayfaceCallback() {
//            @Override
//            public void response(Map map) {
//                Log.e(TAG, JsonHelper.toJson(map));
//                if (codeInfo(map)) {
//                    isAvailable = true;
//                    Log.e(TAG, "初始化成功");
//                }
//            }
//        });
    }

    public void getWxPayRawData() {
//        WxPayFace.getInstance().getWxpayfaceRawdata(new IWxPayfaceCallback() {
//
//            @Override
//            public void response(Map info) {
//                Log.e(TAG, JsonHelper.toJson(info));
//                if (!codeInfo(info)) {
//                    return;
//                }
//                getAuthInfo((String)info.get("rawdata"));
//            }
//        });
    }

    private boolean codeInfo(Map info) {
        String temp = (String) info.get("return_code");
        if (TextUtils.equals(temp, "SUCCESS")) {
            return true;
        }else {
            int code = (int) info.get("err_code");
            if(code==NO_INIT_CODE){
                initWx();
            }
        }
        return false;
    }


    private void getAuthInfo(String rawdata) {
        Map<String, Object> temp = new HashMap<>();
        temp.put("deviceId", APPDidUtil.getAppDid());
        temp.put("rawdata", rawdata);
        String sign = JasyptUtil.getInstance().getSign(temp);
        temp.put("sign", sign);
        OkHttpManager.HttpPostJson(HttpApi.AUTHINFO_URL, temp, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "获取RawData" + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "获取RawData" + response);
            }
        });
    }


}
